package com.wsoft.controller;

import com.wsoft.query.rules.CodeRulesInsertQuery;
import com.wsoft.query.rules.CodeRulesListQuery;
import com.wsoft.query.rules.CodeRulesUpdateQuery;
import com.wsoft.query.rules.InitCodeQuery;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import com.wsoft.service.ITsCodeRulesService;
import com.wsoft.utils.CodeRulesUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 编码规则表 前端控制器
 * @author z001
 * @since 2024年10月16日 13:05:39
 */
@RestController
@Tag(name = "编码规则")
@RequestMapping("/code")
public class TsCodeRulesController {

    @Resource
    private ITsCodeRulesService codeRulesService;
    @Resource
    CodeRulesUtil codeRulesUtil;

    @Operation(summary = "生成code", description = "生成code")
    @PostMapping("/initCode")
    public ResponseResult initCode(@RequestBody @Valid InitCodeQuery query) {
        Object value = codeRulesUtil.initCode(query.getCode(), query.getObjectJson(), query.getIsUsed());
        return ResponseResult.success(value);
    }

    //####################################################################################

    @Operation(summary = "新增规则", description = "新增规则")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid CodeRulesInsertQuery query) {
        boolean b = codeRulesService.insertCodeRulers(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改规则", description = "修改规则")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid CodeRulesUpdateQuery query) {
        boolean b = codeRulesService.updateCodeRulers(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "删除规则", description = "删除规则")
    @GetMapping("/delete")
    public ResponseResult dropColumn(@RequestParam("id") Long id) {
        boolean b = codeRulesService.deleteCodeRulers(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "查询规则列表", description = "查询规则列表")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody CodeRulesListQuery query) {
        return ResponseResult.success(ResponsePage.restPage(codeRulesService.listCodeRulers(query)));
    }

    @Operation(summary = "详情页面", description = "详情页面")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(codeRulesService.getRulesInfo(id));
    }

}

